package com.example.day6_viewpager.fragment;


import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.day6_viewpager.MainActivity;
import com.example.day6_viewpager.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * A simple {@link Fragment} subclass.
 */
public class WelcomeFragmentThree extends Fragment {

    ImageView imageView;
    TextView textView;
    Timer timer;
    int position = 5;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what == 101){
                textView.setText("倒计时："+position--+"");
                if(position == 0){
                    Intent intent = new Intent(getActivity(), MainActivity.class);
                    startActivity(intent);
                    timer.cancel();
                }
            }
        }
    };
    public WelcomeFragmentThree() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_one_fragment_one, container, false);
        imageView = view.findViewById(R.id.iv);
        textView = view.findViewById(R.id.tv);
        Glide.with(getContext()).load("http://photocdn.sohu.com/20110716/Img313617599.jpg").into(imageView);
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(101);

            }
        },1000,1000);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
                timer.cancel();
            }
        });

        return view;
    }

}
